#!/bin/bash
# Installation script
# v1.0.0
#########################################
#instalar guacamole
#instalar soporte para todos los idiomas en firefox
#realizar script de instalacion para el server

#########################################

#########################################
if [ $USER != "root" ]; then
 echo "This script must be run as root"
 exit -1
fi;

#Update
if [ -z "`grep Scriptaddedrepos /etc/apt/sources.list`" ]; then
 cp /etc/apt/sources.list /etc/apt/sources.list.old
 echo "#Scriptaddedrepos
" > /etc/apt/sources.list
 cat /etc/apt/sources.list.old | sed 's/#\s*deb/deb/g' >> /etc/apt/sources.list

else
 echo "Sources.list already modified"

fi;

apt-get update
apt-get -y upgrade

#Make net to connect at startup and get the correct IP
if [ -z "`grep Scriptaddedifs /etc/network/interfaces`" ]; then
 
 sudo apt-get -y remove --purge network-manager

 /etc/init.d/networking stop

 cp /etc/network/interfaces /etc/network/interfaces.old
 if [ "`uname -n`" = "jesus" ]; then

  echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.100
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback
  
   iface eth0 inet dhcp" > /etc/network/interfaces
 
 elif [ "`uname -n`" = "job" ]; then
    echo " #Scriptaddedifs
   auto wlan1
   iface wlan1 inet static
   address 192.168.1.110
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback
  
   iface eth0 inet dhcp" > /etc/network/interfaces

 elif [ "`uname -n`" = "abel" ]; then
    echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.111
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback
  
   iface eth0 inet dhcp" > /etc/network/interfaces

 elif [ "`uname -n`" = "noe" ]; then
    echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.112
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback" > /etc/network/interfaces

 elif [ "`uname -n`" = "adan" ]; then
    echo "#Scriptaddedifs
auto wlan0
iface wlan0 inet static
address 192.168.1.113
netmask 255.255.255.0
gateway 192.168.1.1
pre-up sleep 20
wpa-passphrase soie_caritas
wpa-driver wext
wpa-key-mgmt WPA-PSK
wpa-proto WPA
wpa-ssid SOIE_CARITAS
 
auto lo
iface lo inet loopback

iface eth0 inet dhcp" > /etc/network/interfaces

 elif [ "`uname -n`" = "sanson" ]; then
    echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.114
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback
  
   iface eth0 inet dhcp" > /etc/network/interfaces

 elif [ "`uname -n`" = "moises" ]; then
    echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.115
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback" > /etc/network/interfaces

 elif [ "`uname -n`" = "abraham" ]; then

  echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.116
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback
  
   iface eth0 inet dhcp" > /etc/network/interfaces

 elif [ "`uname -n`" = "abba" ]; then
    echo " #Scriptaddedifs
   auto wlan0
   iface wlan0 inet static
   address 192.168.1.117
   netmask 255.255.255.0
   gateway 192.168.1.1
   pre-up sleep 20
   wpa-passphrase soie_caritas
   wpa-driver wext
   wpa-key-mgmt WPA-PSK
   wpa-proto WPA
   wpa-ssid SOIE_CARITAS
 
   auto lo
   iface lo inet loopback
  
   iface eth0 inet dhcp" > /etc/network/interfaces

 else
  echo "Unhandled system"

 fi;

 cp /etc/resolv.conf /etc/resolv.conf.old
 echo "#Scriptaddedifs
nameserver 8.8.8.8
nameserver 8.8.4.4" > /etc/resolv.conf
 
 /etc/init.d/networking restart

 sleep 100
fi;


#Install basic packages
apt-get -y install expect
apt-get -y install samba
apt-get -y install ntfsprogs ntfs-3g
apt-get -y install openssh-server
apt-get -y install cups

#Add new unprivileged user (with net and mount usb)
if [ -z "`grep ^sanbruno: /etc/passwd`" ]; then
 echo 'spawn adduser sanbruno
  expect "password:"
  send "sanbruno\r"
  expect "password:"
  send "sanbruno\r"

  expect "Full Name []:"
  send "sanbruno\r"
  expect "Room Number []:"
  send "na\r"
  expect "Work Phone []:"
  send "0\r"
  expect "Home Phone []:"
  send "0\r"
  expect "Other []:"
  send "0\r"
  expect "n]:"
  send "S\r"
  expect eof' > /tmp/expect.cmd
 expect -f /tmp/expect.cmd
 rm /tmp/expect.cmd

 groups sanbruno
 usermod -G cdrom,audio,video,plugdev,sambashare sanbruno

else
 echo "Sanbruno user already added"

fi;

#Change the passwords
echo 'spawn passwd root
  expect "UNIX:"
  send "j0s3lu1s\r"
  expect "UNIX:"
  send "j0s3lu1s\r"
  expect eof' > /tmp/expect.cmd
expect -f /tmp/expect.cmd
rm /tmp/expect.cmd

echo 'spawn passwd administrador
  expect "UNIX:"
  send "4dm1n1str4d0r\r"
  expect "UNIX:"
  send "4dm1n1str4d0r\r"
  expect eof' > /tmp/expect.cmd
expect -f /tmp/expect.cmd
rm /tmp/expect.cmd

#Add printers
/etc/init.d/cups restart
if [ -z "`grep 192.168.1.100 /etc/cups/printers.conf`" ]; then
 cp /etc/cups/printers.conf /etc/cups/printers.conf.old
 echo "<DefaultPrinter Impresora>
Info HP Deskjet D1500 series
Location jesus
DeviceURI ipp://192.168.1.100:631/printers/Deskjet-D1500-series
State Idle
StateTime 1271095536
Accepting Yes
Shared No
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy retry-job
</Printer>
" > /etc/cups/printers.conf
 /etc/init.d/cups restart

else
 echo "Printers already added"

fi;

#Change fstab
if [ -z "`grep share /etc/fstab`" ]; then
 cp /etc/fstab /etc/fstab.old
 mkdir /media/compartido
 chmod 777 /media/compartido
 echo "//192.168.1.100/share	/media/compartido	cifs	username=sanbruno,password=sanbruno,auto,exec,user	0	0" >> /etc/fstab
 mount -a

else
 echo "Fstab already modified"

fi;

#Install spanish locale in openoffice
apt-get -y install openoffice.org.*-es

#Configure .bashrc to make some tasks at init
 #Make shortcuts in desktop
 #Remove hist and cookies from navigator
if [ ! -e "/etc/init.d/startupConfigScript" ]; then #MAL
 #TODO: Anadir las configuraciones necesarias
 mkdir /var/bookmarks
 echo ""> /var/bookmarks/lala.html
 #TODO:Make these bookmarks!
 #TODO:Put system, trash and compartido in desktop
 echo '!/bin/bash
 #Startup actions for all users
 
 ## Update
 apt-get update
 apt-get upgrade

 ## Remove temp files for firefox
 cd /home/sanbruno/.mozilla/firefox
 cd *.default
 mv prefs.js ..
 rm -rf *
 cp ../prefs.js .
 echo "user_pref(\"browser.startup.homepage\", \"http://www.google.es\");" >> prefs.js
 echo "user_pref(\"browser.tabs.forceHide\", false);" >> prefs.js

 ## Remove everything from desktop
 DATE=`date +%N`
 mkdir /tmp/${DATE}backup
 mv /home/sanbruno/Escritorio/* /tmp/${DATE}backup

 ## Update if necessary
 wget http://192.168.1.100/update.sh /tmp/update.sh
 /tmp/update.sh
'> /etc/init.d/startupConfigScript
 chmod +x /etc/init.d/startupConfigScript
 update-rc.d startupConfigScript defaults
fi;

#Install needed applications
apt-get -y install flashplugin-nonfree
apt-get -y install vlc mozilla-plugin-vlc
apt-get -y remove totem.* totem-common.* .*brasero.*
apt-get -y install unrar rar p7zip p7zip-full p7zip-rar
apt-get -y install amarok
apt-get -y install k3b
apt-get -y install ntp
apt-get -y install sun-java6-jdk

#Configure just a little
 #Disable os sounds
gconftool-2 -s -t bool /desktop/gnome/sound/event_sounds false
 #TODO:Disable backlight reduce
gconftool-2 -s -t int /desktop/gnome/session/idle_delay 120

#/schemas/apps/gnome-power-manager/backlight
 #Disable hw sounds



#Install admin applications
apt-get -y install tkdiff gparted mercurial vim nmap ncdu wget
gconftool-2 -s -t bool /desktop/gnome/remote_access/enabled true
gconftool-2 -s -t bool /desktop/gnome/remote_access/prompt_enabled false
gconftool-2 -s -t string /desktop/gnome/remote_access/vnc_password j0s3lu1s
apt-get -y install alien-arena

